package com.example.testappwidget.adpter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.testappwidget.R

/**
 *Author: chinadragon
 *Time: 2024/7/8
 */
class SlidingPaneLeftMenuAdapter(
    var mContext: Context,
    val mData: MutableList<String> = mutableListOf(),
    val callBack: (Int) -> Unit
) : RecyclerView.Adapter<SlidingPaneLeftMenuAdapter.SlidingPaneLeftMenuViewHolder>() {
    inner class SlidingPaneLeftMenuViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tv_menu = itemView.findViewById<TextView>(R.id.tv_menu)
    }

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): SlidingPaneLeftMenuViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_sliding_pane_left_menu, parent, false)
        return SlidingPaneLeftMenuViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(holer: SlidingPaneLeftMenuViewHolder, position: Int) {
       holer.tv_menu.text = mData[position]
       holer.itemView.setOnClickListener {
           callBack(position)
       }
    }

    fun setData(data: List<String>?) {
        if (data.isNullOrEmpty()) {
            return
        }
        mData.clear()
        mData.addAll(data)
        notifyDataSetChanged()
    }
}